package jdbc.datasource;


import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.junit.jupiter.api.Test;

import java.io.FileInputStream;
import java.sql.*;
import java.util.Properties;

/**
 * @author DCSGO
 * @version 1.0
 * C3P0的使用
 */
public class C3P0_ {
    //1.代码中对C3P0显示的进行配置
    @Test
    public void testC3P0_1() throws Exception {
        //读取配置信息
        Properties properties = new Properties();
        properties.load(new FileInputStream("src\\mysql.properties"));
        String Driver = properties.getProperty("Driver");
        String url = properties.getProperty("url");
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");

        //1.创建一个数据源对象
        ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
        //2.设置相关信息
        comboPooledDataSource.setDriverClass(Driver);
        comboPooledDataSource.setJdbcUrl(url);
        comboPooledDataSource.setUser(user);
        comboPooledDataSource.setPassword(password);
        comboPooledDataSource.setInitialPoolSize(10);//设置初始连接池大小
        comboPooledDataSource.setMaxPoolSize(50);//设置最大连接池大小
        //3.获取连接
        System.out.println("开始连接...");
        long start = System.currentTimeMillis();
        for (int i = 0; i < 5000; i++) {
            Connection connection = comboPooledDataSource.getConnection();
            connection.close();
        }
        long end = System.currentTimeMillis();
        System.out.println("耗时：" + (end - start));
        /*
         * 十月 02, 2023 11:40:09 上午 com.mchange.v2.log.MLog <clinit>
         * 信息: MLog clients using java 1.4+ standard logging.
         * 十月 02, 2023 11:40:09 上午 com.mchange.v2.c3p0.C3P0Registry banner
         * 信息: Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]
         * 开始连接...
         * 十月 02, 2023 11:40:09 上午 com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource getPoolManager
         * 信息: Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> 1hgex35ay1u6126t12c6xp2|481a15ff, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.jdbc.Driver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1hgex35ay1u6126t12c6xp2|481a15ff, idleConnectionTestPeriod -> 0, initialPoolSize -> 10, jdbcUrl -> jdbc:mysql://localhost:3306/chapter25?rewriteBatchedStatements=true, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 50, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 3, numHelperThreads -> 3, numThreadsAwaitingCheckoutDefaultUser -> 0, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false ]
         * 耗时：378
         *
         * 进程已结束，退出代码为 0
         */
    }

    //通过配置文件设置C3P0
    @Test
    public void testC3P0_2() throws SQLException {
        //1.在src下创建一个名为：c3p0-config.xml 的配置文件
        //2.在其中添加相应的配置信息
        //3.创建数据源对象
        ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource("csgo");//配置名
        //4.获取连接
        System.out.println("开始连接...");
        long start = System.currentTimeMillis();
        for (int i = 0; i < 5000; i++) {
            Connection connection = comboPooledDataSource.getConnection();
            connection.close();
        }
        long end = System.currentTimeMillis();
        System.out.println("耗时：" + (end - start));
        /*
         * 十月 03, 2023 8:20:24 上午 com.mchange.v2.log.MLog <clinit>
         * 信息: MLog clients using java 1.4+ standard logging.
         * 十月 03, 2023 8:20:25 上午 com.mchange.v2.c3p0.C3P0Registry banner
         * 信息: Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]
         * 开始连接...
         * 十月 03, 2023 8:20:25 上午 com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource getPoolManager
         * 信息: Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 5, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> csgo, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.jdbc.Driver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1hgex35ay1vec1ouxitnr5|727803de, idleConnectionTestPeriod -> 0, initialPoolSize -> 10, jdbcUrl -> jdbc:mysql://127.0.0.1:3306/chapter25, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 50, maxStatements -> 5, maxStatementsPerConnection -> 2, minPoolSize -> 5, numHelperThreads -> 3, numThreadsAwaitingCheckoutDefaultUser -> 0, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false ]
         * 耗时：681
         *
         * 进程已结束，退出代码为 0
         */
    }
}
